<?php

namespace yak\layui\widgets\grid;

use yii\grid\Column;
use yii\helpers\ArrayHelper;

class LayuiSerialColumn extends Column implements LayuiColInterface
{
    /**
     * {@inheritdoc}
     */
    public $attribute = '_serial';

    /**
     * @var string
     */
    public $header = '#';

    public function renderDataCell($model, $key, $index)
    {
        $pagination = $this->grid->dataProvider->getPagination();
        if ($pagination !== false) {
            return $pagination->getOffset() + $index + 1;
        }

        return $index + 1;
    }

    public function generateCol(): array
    {
        // TODO: Implement generateCol() method.
        $colOptions = ArrayHelper::getValue($this->options, 'colOptions', []);
        $colOptions['field'] = $this->attribute;
        $colOptions['title'] = $this->header;
        return $colOptions;
    }

    public function getLabel(): string
    {
        // TODO: Implement getLabel() method.
        return $this->header;
    }
}